define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('split_table'));

            utils.addFilterGroupbtn(pane, {
                text: '全部',
                name: 'userType',
                filters: [{text: '淘宝', value: '0'}, {text: '天猫', value: '1'}]
            });

            utils.addFilterGroupbtn(pane, {
                text: '新品',
                name: 'isNew',
                filters: dicts.boolean
            });

            utils.addFilterGroupbtn(pane, {
                text: '人气',
                name: 'isHot',
                filters: dicts.boolean
            });

            utils.addFilterGroupbtn(pane, {
                text: '聚划算',
                name: 'isJhs',
                filters: dicts.boolean
            });

            utils.addFilterGroupbtn(pane, {
                text: '淘抢购',
                name: 'isTqg',
                filters: dicts.boolean
            });

            utils.addFilterGroupbtn(pane, {
                text: '品牌',
                name: 'isPP',
                filters: dicts.boolean
            });

            utils.addFilterGroupbtn(pane, {
                text: '推荐',
                name: 'feature',
                filters: dicts.boolean
            });
        }

        /**
         * 分类
         */
        {
            //全剧变量
            var menu = $('.left-menu', pane).addClass('filter-list').attr('data-name', 'category_id').attr('data-value', 'all');

            //初始化Tree
            menu.jstree({
                core: {
                    data: function (obj, callback) {
                        adminApi('categories_tree').then(function (data) {
                            menu.data = $.extend(true, [], data);
                            utils.handleTreeNodes(pane, true)(data);
                            callback(data);
                        });
                    },
                    check_callback: function (opt, from, to) {
                        //判断是不是可移动
                        return false;
                    }
                },
                plugins: ['dnd', 'contextmenu'],
                contextmenu: {
                    select_node: false,
                    items: function (node) {
                    }
                }
            }).on('changed.jstree', function (e, data) {
                //点击事件
                var id = data.selected[0];
                if (isNaN(id)) {
                    menu.attr('data-value', 'all');
                } else {
                    menu.attr('data-value', id);
                }
                if (id) {
                    table.api().draw();
                }
            }).on("loaded.jstree", function (e, data) {
                //展开节点
                menu.jstree("open_node", '#1');
            }).on("refresh.jstree", function (e, data) {
                menu.jstree("open_node", '#1');
                if (menu.need_open_node) {
                    menu.jstree("open_node", menu.need_open_node);
                    menu.need_open_node = false;
                }
            });
        }


        /**
         * 商品
         */
        var goods_fields_arrays = [
            ['auctionId', 'text', '商品ID', {notEmpty: {message: '商品ID不能为空'}}],
            ['activityId', 'text', '优惠券编号'],
            ['intro', 'textarea', '促销描述']
        ];
        var goodsAddFields = utils.convertFields(goods_fields_arrays);

        //初始化工具
        utils.addTools(pane, [
            {className: 'add-goods-btn', icon: 'fa-plus', text: '添加商品'},
            {className: 'refresh-btn', icon: 'fa-refresh', text: '刷新'}
        ]);
        $(pane).on('click', '.add-goods-btn', function () {
            utils.formAdd(table, {
                name: '商品', fields: goodsAddFields, columns: 6,
                fnServerData: function (paramData, form) {
                    return adminApi('goodses_add', paramData);
                }
            });
        }).on('click', '.refresh-btn', function () {
            if (table) {
                table.api().draw();
                analyse();
            }
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["id", "#", true],
                ["pictUrl", "图片", false, function (image, t, row) {
                    if (!image) return '';
                    return '<a target="_blank" href="' + row.auctionUrl + '"><img src="' + image + '?imageView2/1/w/150/h/150/q/75"></a>';
                }],
                ["title", "商品名", false, function (title, t, row) {
                    return '<a target="_blank" href="/detail?id=' + row.auctionId + '" style="max-width: 240px; display: block;">' + (title || '') + '</a><div>' + row.sources + '</div>' +
                        (row.activityId ? '<a target="_blank" href="https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=' + row.activityId + '&seller_id=' + row.sellerId + '">优惠券: ' + row.quanAmount + '<br>' + row.activityId + '</a>' : '') +
                        ((row.couponInfo && row.couponInfo != '无') ? '<div>联盟券: ' + row.couponInfo + '</div>' : '');
                }],
                ["zkPrice", "售价", true, utils.priceRender()],
                ["quanAmount", "优惠券", true, utils.priceRender()],
                ["quanPrice", "券后价", true, utils.priceRender()],
                ["tkRate", "通用", true, utils.priceRender()],
                ["eventRate", "定向", true, utils.priceRender()],
                ["sellerId", "店铺", false, function (i, t, row) {
                    return (row.nick || row.sellerId) + '<div>' + (row.userType == 0 ? '<span style="color: #FF3B04;">淘宝</span>' : '<span style="color: #BA0003;">天猫</span>') + '</div>';
                }, 'filterable'],
                ["isJhs", "聚划算", true, utils.dictRender('boolean')],
                ["isTqg", "淘抢购", true, utils.dictRender('boolean')],
                ["isPP", "品牌", true, utils.dictRender('boolean')],
                ["feature", "推荐", true, utils.dictRender('boolean')],
                ["order_by", "排序", true],
                ["biz30day", "销量", true],
                ["updated_at", "更新时间", true, utils.dateTimeRender()],
                ["created_at", "添加时间", true, utils.dateTimeRender()]
            ],
            sorting: [[0, 'desc']],
            fnServerData: function (paramData) {
                return adminApi('goodses_browse', paramData);
            },
            fixedHeader: false
        });


        //编辑开关
        var checkedValue = 0;
        var toggleHandler = utils.toggleChild(pane, 1, function (checked) {
            //处理是报价是重新加载数据
            if (checked > 1 && checkedValue != checked) {
                checkedValue = checked;
                setTimeout(function () {
                    table.api().draw(false)
                }, 200);
                return false;
            }
            checkedValue = checked;

            table.api().rows().every(function (idx) {
                var child = this.child, data = this.data(), html = '';
                if (checked == 1) {
                    var opts = [];
                    if (data.feature) {
                        opts.push({text: '取消推荐', className: 'unfeature-opt btn-warning'});
                    } else {
                        opts.push({text: '启用推荐', className: 'feature-opt btn-success'});
                    }
                    if (data.isPP) {
                        opts.push({text: '取消品牌', className: 'cancelPP-opt btn-warning'});
                    } else {
                        opts.push({text: '启用品牌', className: 'isPP-opt btn-success'});
                    }
                    if (data.isJhs) {
                        opts.push({text: '取消聚划算', className: 'cancelJhs-opt btn-warning'});
                    } else {
                        opts.push({text: '启用聚划算', className: 'isJhs-opt btn-success'});
                    }
                    if (data.isTqg) {
                        opts.push({text: '取消淘抢购', className: 'cancelTqg-opt btn-warning'});
                    } else {
                        opts.push({text: '启用淘抢购', className: 'isTqg-opt btn-success'});
                    }
                    opts.push({text: '删除商品', icon: 'fa-trash-o', className: 'delete-ope btn-danger'});
                    child(utils.childWrapper(idx, opts, html)).show();
                } else {
                    if (html) {
                        child(html).show();
                    } else {
                        child.hide();
                    }
                }
            });
        });

        //表格事件
        table.on('draw.dt', function () {
            toggleHandler();
        }).on('click', '.delete-ope', utils.tableEvent(table, function (idx, rowData) {
            //删除商品
            utils.confirmation(this, {
                placement: 'left',
                optName: '删除商品',
                title: '你确定删除商品?',
                fnConfirm: function () {
                    return adminApi('goodses_delete', {id: rowData.id}).then(utils.handDelete(table, {name: '商品'}));
                }
            });
        })).on('click', '.feature-opt', utils.tableEvent(table, function (idx, rowData) {
            //推荐商品
            return adminApi('goodses_edit', {
                id: rowData.id,
                feature: 1
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        })).on('click', '.unfeature-opt', utils.tableEvent(table, function (idx, rowData) {
            //取消推荐商品
            return adminApi('goodses_edit', {
                id: rowData.id,
                feature: 0
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        })).on('click', '.cancelPP-opt', utils.tableEvent(table, function (idx, rowData) {
            //取消品牌
            return adminApi('goodses_edit', {
                id: rowData.id,
                isPP: 0
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        })).on('click', '.isPP-opt', utils.tableEvent(table, function (idx, rowData) {
            //启用品牌
            return adminApi('goodses_edit', {
                id: rowData.id,
                isPP: 1
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        })).on('click', '.cancelJhs-opt', utils.tableEvent(table, function (idx, rowData) {
            //取消聚划算
            return adminApi('goodses_edit', {
                id: rowData.id,
                isJhs: 0
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        })).on('click', '.isJhs-opt', utils.tableEvent(table, function (idx, rowData) {
            //启用聚划算
            return adminApi('goodses_edit', {
                id: rowData.id,
                isJhs: 1
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        })).on('click', '.cancelTqg-opt', utils.tableEvent(table, function (idx, rowData) {
            //取消淘抢购
            return adminApi('goodses_edit', {
                id: rowData.id,
                isTqg: 0
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        })).on('click', '.isTqg-opt', utils.tableEvent(table, function (idx, rowData) {
            //启用淘抢购
            return adminApi('goodses_edit', {
                id: rowData.id,
                isTqg: 1
            }).then(utils.handEdit(table, {idx: idx, name: rowData.title}, true, toggleHandler));
        }));

        /**
         * 初始化分屏
         */
        {
            var splitLeft = $('.split-left', pane);
            utils.initSplit(pane.attr('id'), {
                sizes: [10, 90],
                minSize: 6,
                gutterSize: 6,
                onDrag: function () {
                    var width = splitLeft.width();
                    if (width < 30) {
                        menu.hide();
                    } else {
                        menu.show();
                    }
                }
            });
        }
    }
});